package com.vakitdaraliyor.www.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.vakitdaraliyor.www.dao.CountdownDao;
import com.vakitdaraliyor.www.entity.Countdown;
import com.vakitdaraliyor.www.entity.User;

/**
 * Servlet implementation class GeciciServlet
 */
@WebServlet("/Admin")
public class AdminPanel extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	private CountdownDao countdownDao;
	private List<Countdown> gerisayimListesi = new ArrayList<Countdown>();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminPanel() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		gerisayimListesi = countdownDao.hepsiniGetir();
		request.setAttribute("tarihListesi", gerisayimListesi);
		HttpSession session = request.getSession(false);
		User user = null;
		if (session != null)
			user = (User) session.getAttribute("user");
		
		if(user!=null && user.getRole().getRoleName().equals("admin")){
			request.getRequestDispatcher("/admin/yoneticiPaneli.jsp").forward(request,response);
		} else {
			response.sendRedirect(request.getContextPath() + "/countDown");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
